<template>
<div>
  <div v-if="bidProject" class="tl-form-line-title sub-title tl-form" style="height: auto;width: 100%;line-height: 28px;">是否存在评分细则：
    <el-select v-model="projectinfo.existbidscoresecond" filterable :disabled="true">
      <el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="item.code" />
    </el-select>
  </div>
  <div v-if="projectinfo.existbidscoresecond === '1'">
  <el-collapse v-model="activeNames">
    <el-collapse-item title="专家拜访" name="expert" class="colitem">
      <tl-edit-list ref="editListForm" :bidscoretimes="'2'" :projectinfo="projectinfo"></tl-edit-list>
    </el-collapse-item>
  </el-collapse>
  <div v-show="false">
      <tl-line :ref="'line2'" :times="'1'" @isloading="isloading" :isapproval="false" :biddate="biddatefortime1" :editdisabled="false" :type="'2'" :projectinfo="projectinfo" :title="'技术'" @showFullScreen="showFullScreen('line2')"></tl-line>
  </div>
<div v-if="secondshow" style="position: relative">
  <div class="subtitle-button" style="float: right">
    <tl-attachment ref="secondAttachmentRef" sourceType="MpmProjectBidScoreSecond" :sourceId="projectinfo.id" :disabled="false" :editable="false"></tl-attachment>
    <el-button type="primary" @click="getScoreIdHis2"><svg-icon icon-class="list" />审批历史</el-button>
  </div>
  <div class="tl-form-line-title">第二次自评（挂网阶段自评）</div>
      <tl-line :ref="'line3'" :times="'2'" @isloading="isloading" :isapproval="false" :biddate="biddatefortime2" :editdisabled="false" :type="'1'" :projectinfo="projectinfo" :title="'商务'" @showFullScreen="showFullScreen('line3')"></tl-line>
      <tl-line :ref="'line4'" :times="'2'" @isloading="isloading" :isapproval="false" :biddate="biddatefortime2" :editdisabled="false" :type="'2'" :projectinfo="projectinfo" :title="'技术'" @showFullScreen="showFullScreen('line4')"></tl-line>
      <tl-offer :ref="'line5'" :times="'2'" :editable="secondedit" :type="'3'" :projectinfo="projectinfo" :title="'报价'"></tl-offer>
  </div>
  <tl-edit ref="editForm" :projectinfo="projectinfo"></tl-edit>
  <tl-approval-history ref="history" sourceType="MpmProjectBidScore" :sourceId="scoreid" />
  <tl-line ref="fullScreenRef" :render="'el-dialog'" v-bind="fullScreenProps"  @refresh="refreshScoreList"></tl-line>
  </div>
</div>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import TlRefStakeholder from '../mpmProjectStakeholder/refList'
import TlLine from './indexFrame'
import tlEdit from './edit'
import tlEditList from '@/views/mpm/mpmProjectVisitExpert/editList'
import tlOffer from './indexFrameOffer'

/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { TlRefStakeholder, TlLine, tlEdit, tlEditList, tlOffer },
  data() {
    return {
      moduleTitle: '评标细则信息表',
      urlPrefix: '/mpm/projectBidScore/',
      url: {
        // 额外的url
        queryScoreInfo: '/mpm/projectBidScore/queryScoreInfo/',
        queryCodeList: '/mpm/projectBidScore/dataCodeList',
        queryMpmInfo: '/mpm/projectBidScore/queryMpmInfo/',
        fullScoreDoSave: '/mpm/projectBidFullScore/doSave/',
        getScoreIdHis: '/mpm/projectBidScore/getScoreIdHis'
      },
      externalQueryParam: {
        qry_projectcode: this.projectinfo.projectcode // 项目编号
      },
      isdisbledtime: true,
      firstedit: true,
      secondedit: false,
      secondshow: true,
      loading: true,
      listindex: 0,
      biddatefortime1: new Date(),
      biddatefortime2: new Date(),
      purchasepattern: '0',
      scoreid: 0,
      firsthis: false,
      secondhis: false,
      targetscoretotal: '0',
      selfscoretotal: '0',
      isfirstpublish: true,
      issecondpublish: true,
      fullScreenProps: {},
      fullScreenRefName: '',
      activeNames: ['expert']
    }
  },
  props: {
    projectinfo: {}
  },
  computed: {
    bidProject() {
      return this.purchasepattern === '11' || this.purchasepattern === '12' || this.purchasepattern === '13'
    }
  },
  methods: {
    showFullScreen(refName) {
      this.fullScreenProps = this.$refs[refName].$options.propsData
      this.fullScreenRefName = refName
      this.$refs.fullScreenRef.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.fullScreenRef.loadData({})
      })
    },
    refreshScoreList() {
      this.$refs[this.fullScreenRefName].loadData({})
    },
    doSave() {
      let fullScorelist = {
        scoreMap1: {
          times: '1',
          type: '1',
          projectcode: this.projectinfo.projectcode,
          totalfullsocre: this.$refs.line1 ? this.$refs.line1.totalfullscore : ''
        },
        scoreMap2: {
          times: '1',
          type: '2',
          projectcode: this.projectinfo.projectcode,
          totalfullsocre: this.$refs.line2 ? this.$refs.line2.totalfullscore : ''
        },
        scoreMap3: {
          times: '2',
          type: '1',
          projectcode: this.projectinfo.projectcode,
          totalfullsocre: this.$refs.line3 ? this.$refs.line3.totalfullscore : '100'
        },
        scoreMap4: {
          times: '2',
          type: '2',
          projectcode: this.projectinfo.projectcode,
          totalfullsocre: this.$refs.line4 ? this.$refs.line4.totalfullscore : '100'
        }
      }
      this.saveLoading = true
      // 增加唯一单号
      postAction(this.url.fullScoreDoSave, fullScorelist)
        .then((res) => {
          if (res.success) {
            this.$tap.message('保存成功', 'success')
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
     /** 第一次自评发布 */
    doSubmitApproval1() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      if (!this.beforeApproval()) {
        return
      }
      let tipInfo = '确定发布吗？'
      // 第一次自评发布
      let first = true
      if (this.$refs.line2.fullscoretotal > parseFloat(this.$refs.line2.totalfullscore)) {
        this.$tap.alert('第一次自评-技术满分大于满分合计，不允许发布！')
        first = false
      }
      if (this.$refs.line1.fullscoretotal > parseFloat(this.$refs.line1.totalfullscore)) {
        this.$tap.alert('第一次自评-商务满分大于满分合计，不允许发布！')
        first = false
      }
      if (!first) {
        this.submitLoading = false
        return
      }
      if (this.$refs.line2.fullscoretotal < parseFloat(this.$refs.line2.totalfullscore)) {
        tipInfo = '当前已填技术满分和小于技术总分，还可继续新增技术自评，确认发布吗？'
      }
      if (this.$refs.line1.fullscoretotal < parseFloat(this.$refs.line1.totalfullscore)) {
        tipInfo = '当前已填商务满分和小于商务总分，还可继续新增商务自评，确认发布吗？'
      }
      let key = true
      if (this.$refs.line1.datas.length > 0) {
          for (let i = 0; i < this.$refs.line1.datas.length; i++) {
          if (this.$refs.line1.datas[i].state === 0 || this.$refs.line1.datas[i].state === 2) {
            this.$set(this, 'model', this.$refs.line1.datas[i])
            key = false
            break
          }
        }
      }
      if (this.$refs.line2.datas.length > 0) {
        for (let i = 0; i < this.$refs.line2.datas.length; i++) {
          if (this.$refs.line2.datas[i].state === 0 || this.$refs.line2.datas[i].state === 2) {
            this.$set(this, 'model', this.$refs.line2.datas[i])
            key = false
            break
          }
        }
      }
      if (key) {
        this.$tap.alert('当前无可发布内容，请确认！')
        this.submitLoading = false
        return
      }
      this.submitLoading = true
      this.$tap
        .confirm(tipInfo)
        .then(_ => {
          this.doSave()
          putAction(this.url.submit, this.model).then(res => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.queryScoreInfo()
              this.$refs.line1.loadData({})
              this.$refs.line2.loadData({})
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch(_ => {
          this.submitLoading = false
        })
    },
        /** 提交审批 */
    doSubmitApproval2() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      if (!this.beforeApproval()) {
        return
      }
      let tipInfo = '确定发布吗？'
      // 第二次自评发布
      if (this.secondshow) {
        let second = true
        if (this.$refs.line3.fullscoretotal > parseFloat(this.$refs.line3.totalfullscore)) {
          this.$tap.alert('第二次自评-商务满分大于满分合计，不允许发布！')
          second = false
        }
        if (this.$refs.line4.fullscoretotal > parseFloat(this.$refs.line4.totalfullscore)) {
          this.$tap.alert('第二次自评-技术满分大于满分合计，不允许发布！')
          second = false
        }
        if (!second) {
          this.submitLoading = false
          return
        }
        if (this.$refs.line3.fullscoretotal < parseFloat(this.$refs.line3.totalfullscore)) {
          tipInfo = '当前已填商务满分和小于商务总分，还可继续新增商务自评，确认发布吗？'
        }
        if (this.$refs.line4.fullscoretotal < parseFloat(this.$refs.line4.totalfullscore)) {
          tipInfo = '当前已填技术满分和小于技术总分，还可继续新增技术自评，确认发布吗？'
        }
        for (let i = 0; i < this.$refs.line3.datas.length; i++) {
          if (!this.$refs.line3.datas[i].planenddate) {
            this.$tap.alert('请填写计划完成日期')
            this.submitLoading = false
            return
          }
        }
        for (let i = 0; i < this.$refs.line4.datas.length; i++) {
          if (!this.$refs.line4.datas[i].planenddate) {
            this.$tap.alert('请填写计划完成日期')
            this.submitLoading = false
            return
          }
        }
      }
      let key = true
      if (this.$refs.line3.datas.length > 0) {
        for (let i = 0; i < this.$refs.line3.datas.length; i++) {
          if (this.$refs.line3.datas[i].state === 0 || this.$refs.line3.datas[i].state === 2) {
            this.$set(this, 'model', this.$refs.line3.datas[i])
            key = false
            break
          }
        }
      }
      if (this.$refs.line4.datas.length > 0) {
        for (let i = 0; i < this.$refs.line4.datas.length; i++) {
          if (this.$refs.line4.datas[i].state === 0 || this.$refs.line4.datas[i].state === 2) {
            this.$set(this, 'model', this.$refs.line4.datas[i])
            key = false
            break
          }
        }
      }
      if (key) {
        this.$tap.alert('当前无可发布内容，请确认！')
        this.submitLoading = false
        return
      }
      this.submitLoading = true
      this.$tap
        .confirm(tipInfo)
        .then(_ => {
          this.doSave()
          putAction(this.url.submit, this.model).then(res => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.queryScoreInfo()
              this.$refs.line3.loadData({})
              this.$refs.line4.loadData({})
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch(_ => {
          this.submitLoading = false
        })
    },
        /** 提交审批 */
    doSubmitApproval() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      if (!this.beforeApproval()) {
        return
      }
      let tipInfo = '确定发布吗？'
      // 第一次自评发布
      if (this.firstedit && this.isfirstpublish) {
        let first = true
        if (this.$refs.line2.fullscoretotal > parseFloat(this.$refs.line2.totalfullscore)) {
          this.$tap.alert('第一次自评-技术满分大于满分合计，不允许发布！')
          first = false
        }
        if (this.$refs.line1.fullscoretotal > parseFloat(this.$refs.line1.totalfullscore)) {
          this.$tap.alert('第一次自评-商务满分大于满分合计，不允许发布！')
          first = false
        }
        if (!first) {
          this.submitLoading = false
          return
        }
        if (this.$refs.line2.fullscoretotal < parseFloat(this.$refs.line2.totalfullscore)) {
          tipInfo = '当前已填技术满分和小于技术总分，还可继续新增技术自评，确认发布吗？'
        }
        if (this.$refs.line1.fullscoretotal < parseFloat(this.$refs.line1.totalfullscore)) {
          tipInfo = '当前已填商务满分和小于商务总分，还可继续新增商务自评，确认发布吗？'
        }
      }
      // 第二次自评发布
      if (this.secondshow) {
        let second = true
        if (this.$refs.line3.fullscoretotal > parseFloat(this.$refs.line3.totalfullscore)) {
          this.$tap.alert('第二次自评-商务满分大于满分合计，不允许发布！')
          second = false
        }
        if (this.$refs.line4.fullscoretotal > parseFloat(this.$refs.line4.totalfullscore)) {
          this.$tap.alert('第二次自评-技术满分大于满分合计，不允许发布！')
          second = false
        }
        if (!second) {
          this.submitLoading = false
          return
        }
        if (this.$refs.line3.fullscoretotal < parseFloat(this.$refs.line3.totalfullscore)) {
          tipInfo = '当前已填商务满分和小于商务总分，还可继续新增商务自评，确认发布吗？'
        }
        if (this.$refs.line4.fullscoretotal < parseFloat(this.$refs.line4.totalfullscore)) {
          tipInfo = '当前已填技术满分和小于技术总分，还可继续新增技术自评，确认发布吗？'
        }
        for (let i = 0; i < this.$refs.line3.datas.length; i++) {
          if (!this.$refs.line3.datas[i].planenddate) {
            this.$tap.alert('请填写计划完成日期')
            this.submitLoading = false
            return
          }
        }
        for (let i = 0; i < this.$refs.line4.datas.length; i++) {
          if (!this.$refs.line4.datas[i].planenddate) {
            this.$tap.alert('请填写计划完成日期')
            this.submitLoading = false
            return
          }
        }
      }
      this.submitLoading = true
      this.$tap
        .confirm(tipInfo)
        .then(_ => {
          if (this.firstedit) {
            let key = true
            if (this.$refs.line1.datas.length > 0) {
               for (let i = 0; i < this.$refs.line1.datas.length; i++) {
                if (this.$refs.line1.datas[i].state === 0 || this.$refs.line1.datas[i].state === 2) {
                  this.$set(this, 'model', this.$refs.line1.datas[i])
                  key = false
                  break
                }
              }
            }
            if (this.$refs.line2.datas.length > 0) {
              for (let i = 0; i < this.$refs.line2.datas.length; i++) {
                if (this.$refs.line2.datas[i].state === 0 || this.$refs.line2.datas[i].state === 2) {
                  this.$set(this, 'model', this.$refs.line2.datas[i])
                  key = false
                  break
                }
              }
            }
            if (key) {
              this.$tap.alert('当前无可发布内容，请确认！')
              this.submitLoading = false
              return
            }
          } else {
            let key = true
            if (this.$refs.line3.datas.length > 0) {
              for (let i = 0; i < this.$refs.line3.datas.length; i++) {
                if (this.$refs.line3.datas[i].state === 0 || this.$refs.line3.datas[i].state === 2) {
                  this.$set(this, 'model', this.$refs.line3.datas[i])
                  key = false
                  break
                }
              }
            }
            if (this.$refs.line4.datas.length > 0) {
              for (let i = 0; i < this.$refs.line4.datas.length; i++) {
                if (this.$refs.line4.datas[i].state === 0 || this.$refs.line4.datas[i].state === 2) {
                  this.$set(this, 'model', this.$refs.line4.datas[i])
                  key = false
                  break
                }
              }
            }
            if (key) {
              this.$tap.alert('当前无可发布内容，请确认！')
              this.submitLoading = false
              return
            }
          }
          this.doSave()
          putAction(this.url.submit, this.model).then(res => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              // if (this.firstedit) {
              //   this.firstedit = false
              //   this.firsthis = true
              //   this.scoreid = this.model.id
              // } else {
              //   this.secondedit = false
              //   this.secondhis = true
              //   this.scoreid = this.model.id
              // }
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch(_ => {
          this.submitLoading = false
        })
    },
    queryMpmInfo() {
      return getAction(this.url.queryMpmInfo, { projectid: this.projectinfo.id, times: '2' }).then((res) => {
          if (res.success) {
            this.purchasepattern = res.result.purchasepattern
            if (res.result.bidbegindate === null) {
                this.biddatefortime1 = new Date(this.projectinfo.planenddate)
              } else {
                this.biddatefortime1 = new Date(res.result.bidbegindate)
            }
            if (res.result.bidenddate === null) {
                this.biddatefortime2 = new Date(this.projectinfo.planenddate)
              } else {
                this.biddatefortime2 = new Date(res.result.bidenddate)
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
    },
    queryScoreInfo() {
      return getAction(this.url.queryScoreInfo, { projectcode: this.projectinfo.projectcode }).then((res) => {
          if (res.success) {
            this.scoreid = res.result.scoreid
            if (this.user.code !== this.projectinfo.projectmanagercode) {
              this.firstedit = false
              this.secondedit = false
              this.secondshow = res.result.secondshow
            } else {
              this.firstedit = res.result.firstedit
              this.secondedit = res.result.secondedit
              this.secondshow = res.result.secondshow
            }
            this.isloading()
            this.scoreid = res.result.scoreid
            this.firsthis = res.result.firsthis
            this.secondhis = res.result.secondhis
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
    },
    isloading() {
      this.listindex++
      let index = 2
      if (!this.secondshow) {
        index = 1
      }
      if (this.listindex >= index) {
        this.loading = false
      }
      if (this.$refs.line1 && this.$refs.line2) {
        this.isfirstpublish = this.$refs.line1.iscanpublish && this.$refs.line2.iscanpublish
      }
      if (this.$refs.line3 && this.$refs.line4) {
        this.issecondpublish = this.$refs.line3.iscanpublish && this.$refs.line4.iscanpublish
      }
    },
    getScoreIdHis1() {
      this.getScoreIdHis('1')
    },
    getScoreIdHis2() {
      this.getScoreIdHis('2')
    },
    getScoreIdHis(times) {
      let param = {
        qry_projectcode: this.projectinfo.projectcode,
        qry_times: times
      }
      getAction(this.url.getScoreIdHis, param).then(res => {
        if (res.success) {
          this.scoreid = res.result.scoreid
          this.$refs.history.visible = true
        } else {
          this.$tap.alert('获取审批历史失败，请联系管理员！', 'warning')
        }
      })
    }
  },
  created() {
    this.queryMpmInfo()
    this.queryScoreInfo()
    this.listindex = 0
  }
}
</script>
<style scoped lang="scss">
.stakeholderForm {
  margin-top: 15px;
  margin-left: -220px;
  .el-row {
    margin-bottom: 2px;
  }
  /* 隐藏禁用状态下的提示 */
  .is-disabled ::placeholder {
    color: transparent !important;
  }
}
.tl-ref-user {
  /deep/ .tl-ref-selector {
    .el-button {
      background-color: red !important;
    }
  }
}
 //将按钮移出文档流，避免占用空间导致标题挤压换行
  .subtitle-button{
    position:absolute;
    right:5px
  }
/deep/ .colitem.el-collapse-item .el-collapse-item__header {
  font-size: 18px !important;
  font-weight: bold;
  height: 20px;
  line-height: 20px;
  border-left: 4px solid #409eff !important;
  padding-left: 8px;
  margin: 17px 0 12px 15px;
  overflow: hidden;
}
</style>
